package org.sixtynine.stock.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.sixtynine.stock.entity.BaseEntity;
import org.sixtynine.stock.entity.Sector;
import org.sixtynine.stock.entity.SubSector;
import org.sixtynine.stock.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Thilan
 * 
 *         this class use to add , edit and delete sub sector data
 * 
 */

@Controller
@SessionAttributes
public class SubSectorController extends BackEndAuthController {

	@Autowired
	private GenericService genericService;

	@RequestMapping(value = "/admin/subsector/add")
	public ModelAndView addSector(HttpServletRequest request) {

		ModelAndView modelAndView = null;

		if (!isUserLogged(request)) {
			modelAndView = new ModelAndView("/login/login");
			return modelAndView;
		}

		modelAndView = new ModelAndView("/admin/subsector/add");
		List<BaseEntity> sectors = genericService.findAll(Sector.class);
		modelAndView.addObject("sectorMap", sectors);
		modelAndView.addObject("subsector", new SubSector());
		return modelAndView;
	}

	@RequestMapping(value = "/admin/subsector/add/process")
	public ModelAndView addingSector(
			@ModelAttribute @Valid SubSector subSector, BindingResult result) {

		if (!result.hasErrors()) {
			genericService.saveOrUpdate(subSector);

			ModelAndView modelAndView = new ModelAndView(
					"/admin/subsector/list");

			List<BaseEntity> subSectors = genericService
					.findAll(SubSector.class);
			modelAndView.addObject("subSectors", subSectors);

			String message = "Team was successfully added.";
			modelAndView.addObject("message", message);

			return modelAndView;
		} else {

			ModelAndView modelAndView = new ModelAndView("/admin/subsector/add");
			return modelAndView;
		}
	}

	@RequestMapping(value = "/admin/subsector/list")
	public ModelAndView listOfSectors() {
		ModelAndView modelAndView = new ModelAndView("/admin/subsector/list");

		List<BaseEntity> subSectors = genericService.findAll(SubSector.class);
		modelAndView.addObject("subSectors", subSectors);

		return modelAndView;
	}

	@RequestMapping(value = "/admin/subsector/edit/{id}", method = RequestMethod.GET)
	public ModelAndView editSector(@PathVariable Integer id) {
		ModelAndView modelAndView = new ModelAndView("/admin/subsector/edit");
		BaseEntity subSector = genericService.findById(id, SubSector.class);
		List<BaseEntity> sectors = genericService.findAll(Sector.class);
		modelAndView.addObject("sectorMap", sectors);
		modelAndView.addObject("subSector", subSector);
		return modelAndView;
	}

	@RequestMapping(value = "/admin/subsector/edit/{id}", method = RequestMethod.POST)
	public ModelAndView edditingSector(
			@ModelAttribute @Valid SubSector subSector, BindingResult result,
			@PathVariable Integer id) {

		if (!result.hasErrors()) {

			genericService.saveOrUpdate(subSector);
			ModelAndView modelAndView = new ModelAndView(
					"/admin/subsector/list");

			List<BaseEntity> subSectors = genericService
					.findAll(SubSector.class);
			modelAndView.addObject("subSectors", subSectors);

			String message = "Sub Sector was successfully edited.";
			modelAndView.addObject("message", message);

			return modelAndView;
		} else {
			ModelAndView modelAndView = new ModelAndView(
					"/admin/subsector/edit");
			List<BaseEntity> sectors = genericService.findAll(Sector.class);
			modelAndView.addObject("sectorMap", sectors);
			return modelAndView;

		}
	}

	@RequestMapping(value = "/admin/subsector/delete/{id}", method = RequestMethod.GET)
	public ModelAndView deleteSector(@PathVariable Integer id) {

		genericService.delete(id, SubSector.class);

		ModelAndView modelAndView = new ModelAndView("/admin/subsector/list");
		List<BaseEntity> subSectors = genericService.findAll(SubSector.class);
		modelAndView.addObject("subSectors", subSectors);

		String message = "Team was successfully deleted.";
		modelAndView.addObject("message", message);
		return modelAndView;
	}
}
